//
//  Rectangle.m
//  魔法砖
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "Rectangle.h"

@implementation Rectangle

//保存数据以字典对形式，然后转换成json格式；
- (NSDictionary *)dictionary
{
    NSNumber *x = @(_point.x);
    NSNumber *y = @(_point.y);
    NSNumber *width = @(_size.width);
    NSNumber *height = @(_size.height);
    NSNumber *color = @(_color);

    return @{@"point":@{@"x":x,@"y":y},
             @"size":@{@"width":width,@"height":height},@"color":color};
}

//将json格式转化成字典，然后转化成对象的属性；
+ (instancetype)rectangleFormDictionary:(NSDictionary *)dict
{
    Rectangle *rectangle = [[Rectangle alloc] init];
    
    NSDictionary *pointDict = dict[@"point"];
    rectangle.point = CGPointMake([pointDict[@"x"] floatValue], [pointDict[@"y"] floatValue]);
    
    NSDictionary *sizeDict = dict[@"size"];
    rectangle.size = CGSizeMake([sizeDict[@"width"] floatValue], [sizeDict[@"height"] floatValue]);
    
    rectangle.color = [dict[@"color"] integerValue];
    return rectangle;
}
@end
